package it.amicosmanettone.whatsimage;

import it.amicosmanettone.whatsimage.client.ApiClient;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class ModifyImageActivity extends Activity {
	
	String imagePath = null;
	ImageView image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_image);
		
		Bundle extras = getIntent().getExtras();

		if (extras != null) {

			imagePath = extras.getString("imagePath");

		}
		
		this.image = (ImageView) findViewById(R.id.modifyImage_image);
		
		boolean downloadImage = ApiClient.getImage(image, imagePath);
		Log.v("ImageDownloader", "Finito scaricamento immagine");
		if (!downloadImage) {

			Log.v("IMM", "SETTO SE NON PRESENTE");
			Drawable res = getResources().getDrawable(R.drawable.no_image);
			image.setImageDrawable(res);

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		getMenuInflater().inflate(R.menu.main, menu);
		/*SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		String userLogged = sharedPref.getString("userLogged", "GUEST");
		
		if(userLogged.equals("GUEST")){
			menu.removeItem(R.id.action_logout);
		}else{
			menu.removeItem(R.id.action_login);
		}*/
		menu.removeItem(R.id.action_login);
		menu.removeItem(R.id.action_logout);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {

			case android.R.id.home:

				return true;
				
			case R.id.action_info:
				
				Intent informationActivity = new Intent(this, InformationActivity.class);
				startActivity(informationActivity);
				overridePendingTransition(R.xml.translate_in, R.xml.translate_out);

				return true;
				
			case R.id.action_login:
				Intent loginActivity = new Intent(this, LoginActivity.class);
				loginActivity.putExtra("fromActivity", "IMAGE");
				startActivity(loginActivity);
				overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
				
				return true;
			
			case R.id.action_logout:
				Intent logoutActivity = new Intent(this, LoginActivity.class);
				logoutActivity.putExtra("fromActivity", "IMAGE");
				startActivity(logoutActivity);
				overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
				
				return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
